<?php
namespace app\admin\model;

use support\think\Model;
use think\facade\Db;
class Menu extends Model
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 数据表名称
     * @var string
     */
    protected $name = 'menus';

    /**
     * 是否需要自动写入时间戳，设置为'int'则为时间戳格式
     * @var bool|string
     */
    protected $autoWriteTimestamp = 'int';

    /**
     * 创建时间字段
     * @var string
     */
    protected $createTime = 'created_at';

    /**
     * 更新时间字段
     * @var string
     */
    protected $updateTime = 'updated_at';
    
    /**
     * 获取某个管理员的菜单树
     * @param int $adminId
     * @return array
     */
    public static function getMenuTreeByAdmin($adminId)
    {
        if ($adminId === 1) {
            $menus = self::where('status', 1)
                ->order('sort', 'asc')
                ->select()
                ->toArray();
        } else {
            $admin = Admin::find($adminId);
            $roleIds = $admin ? $admin->roles()->column('id') : [];
            if (empty($roleIds)) {
                return [];
            }
            $menuIds = Db::name('role_menu')->whereIn('role_id', $roleIds)->column('menu_id');
            if (empty($menuIds)) {
                return [];
            }
            $menus = self::whereIn('id', array_unique($menuIds))
                ->where('status', 1)
                ->order('sort', 'asc')
                ->select()
                ->toArray();
        }
        return self::buildMenuTree($menus);
    }

    /**
     * 构建菜单树
     */
    public static function buildMenuTree(array $menus, int $parentId = 0): array
    {
        $tree = [];
        foreach ($menus as $menu) {
            if ($menu['type'] == 3) {
                continue;
            }
            if ($menu['pid'] == $parentId) {
                unset($menu['updated_at']);
                $authList = [];
                foreach ($menus as $authItem) {
                    if ($authItem['type'] == 3 && $authItem['pid'] == $menu['id']) {
                        $authList[] = [
                            'id' => $authItem['id'],
                            'title' => $authItem['title'],
                            'authMark' => $authItem['permission'] ?? '',
                            'is_menu' => 0,
                            'sort' => $authItem['sort'],
                            'type' => 3,
                        ];
                    }
                }
                $children = self::buildMenuTree($menus, $menu['id']);
                $tree[] = [
                    'id' => $menu['id'],
                    'path' => $menu['path'],
                    'component' => $menu['component'],
                    'name' => $menu['permission'],
                    'meta' => [
                        'id' => $menu['id'],
                        'title' => $menu['title'],
                        'icon' => $menu['icon'],
                        'authList' => $authList,
                        'sort' => $menu['sort'],
                        'status' => (bool)$menu['status'],
                        'type' => $menu['type'],
                    ],
                    'children' => $children,
                ];
            }
        }
        return $tree;
    }
}